package com.service.impl;

import com.mapper.PermissionMapper;
import com.mapper.RoleMapper;
import com.mapper.RolePermissionMapper;
import com.pojo.Permission;
import com.pojo.Role;
import com.service.RoleService;
import com.vo.roleTreeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceimpl implements RoleService {
    @Resource
    public RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Override
    public List<Role> findAllRole() {
        List<Role> roleslist = roleMapper.selectAllRole();
        return roleslist;
    }

 /*   @Override
    public void roleGrant(Integer roleId, Integer[] permIds) {
        //删除原有的权限
        rolePermissionMapper.deletePermissonByRoleId(roleId);
        //添加新的权限
        rolePermissionMapper.addPermissionByRoleId(roleId,permIds);
    }*/

    @Override
    public List<roleTreeVo> queryTreeData(Integer roleId, Integer parentId) {
        //1.根据parentId父id查询子级权限
        List<Permission> permissionList = permissionMapper.selectPermissionByParentId(parentId);
        //2.判断子级权限是否为null
        if(permissionList.size()!=0){
            //3.创建一个TreeVo集合
            List<roleTreeVo> list = new ArrayList<roleTreeVo>();
            //4.循环PermissionList集合，每循环出一个Permission对象， 将其封装到TreeVo中
            for (Permission permission : permissionList) {
                //调用转换方法
                roleTreeVo treeVo = transferTo(roleId,permission);
                //5.将treeVo添加到集合中
                list.add(treeVo);
            }
            return list;
        }else{
            return null;
        }
    }
    /**
     * 转换，将Permission转换为TreeVo
     * @param permission
     * @return
     */
    public roleTreeVo transferTo(Integer roleId,Permission permission){
        roleTreeVo treeVo = new roleTreeVo();
        treeVo.setId(permission.getId());//权限id
        treeVo.setTitle(permission.getPermisName());//权限名称
        //根据角色id查询该角色拥有的权限
        List<Integer> permIdsList = rolePermissionMapper.queryPermissionByRoleId(roleId);
        //根据fuid查询子级权限的数量 ,只用当count=0时，才能确保当前的权限是三级权限，也就是没有子级权限了
        Integer count = permissionMapper.selectCountByParentId(permission.getId());
        //判断该角色的权限id是否在所有权限中包含
        if(permIdsList.contains(permission.getId()) && count == 0){
            treeVo.setChecked(true);
        }else{
            treeVo.setChecked(false);
        }
        //查询当前权限对应的子级权限
        List<roleTreeVo> treeVoList = queryTreeData(roleId,permission.getId());
        //封装到treeVo的子级中
        treeVo.setChildren(treeVoList);
        return treeVo;
    }

    /**
     * 角色授权
     * @param roleId  角色id
     * @param permIds 权限集合
     */
    @Override
    @Transactional
    public void roleGrant(Integer roleId, Integer[] permIds) {
        //删除原有的权限
        rolePermissionMapper.deletePermissonByRoleId(roleId);
        //添加新的权限
        rolePermissionMapper.addPermissionByRoleId(roleId,permIds);
    }
}
